/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package presentacion;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.*;
import org.jfree.data.category.DefaultCategoryDataset;

class GraficoBarras extends java.awt.Frame {

    HashMap datos;
    BufferedImage grafica = null;
    String tipo = "";
    String ano = "";
    String semestreMes = "";
    
    public GraficoBarras() {
        super("Gráfico de Distribución");
        datos = new HashMap();
        this.setLocationRelativeTo(null);
        
        addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                datos.clear();
                setVisible(false);
            }
            
            @Override
            public void windowClosed(WindowEvent e) {
                datos.clear();
                setVisible(false);
            }
        });
    }

    public HashMap getDatos() {
        return datos;
    }

     public BufferedImage creaImagen() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
         Iterator i = datos.entrySet().iterator();  // Get an iterator

        while (i.hasNext()) {
            Map.Entry me = (Map.Entry) i.next();
            String y =  (String)me.getValue();
            String x = (String) me.getKey();
            dataset.setValue(Integer.parseInt(y), "Frecuencia", x);
        }
       
        JFreeChart chart = ChartFactory.createBarChart3D("Título", "Eje X", "Eje Y", 
                dataset, PlotOrientation.VERTICAL, true, true, false);
        chart.setTitle("Gráfico de la Cantidad de Estudiantes que usan las Computadoras del Megalaboratorio Estudiantil de Informática según " + tipo /*+ ", "+ semestreMes +", "+ ano*/);
        BufferedImage image = chart.createBufferedImage(600, 600);
        return image;
    }

    @Override
    public void paint(java.awt.Graphics g) {
        grafica = this.creaImagen();
        g.drawImage(grafica, 30, 30, null);
    }

    public void setAno(String ano) {
        this.ano = ano;
    }

    public void setSemestreMes(String semestreMes) {
        this.semestreMes = semestreMes;
    }

    public void setTipo(String tipo) {
        this.tipo = tipo;
    }    
}



